# Create your views here.
# coding:utf-8
from django.http import HttpResponse
from django.contrib.auth import authenticate  
from django.contrib.auth.tokens import default_token_generator  
from django.core import serializers
import json
 

def login(request):

    if request.method=='GET':
        pass
    if request.method=='POST':
        data = {}
        data['state'] = 'failed'
        data['message'] = u'登录失败'
        data['token'] = ''
        data['user'] = ''
        if 'username' in request.POST and 'password' in request.POST:
			username = request.POST.get('username')
			password = request.POST.get('password')

			user = authenticate(username=username,password=password)
			if user is not None:
				data['state'] = 'sucess'
				data['message'] = u'登录成功'
				data['token'] = default_token_generator.make_token(user)
				data['user'] = user.pk
			else:
				data['message'] = u'用户名或密码错误'
        else:
			data['message'] = u'请求参数错误'
        return HttpResponse(json.dumps(data), content_type="application/json")	

def logout(request):
    if request.method == 'GET':
    	pass
    if request.method == 'POST':
        data = {}
        data['message'] = u'注销失败'
        data['state'] = 'failed'
        if 'user' in request.POST and 'token' in request.POST:
            #此处注销
        	pass
        else:
            data['message'] = u'请求参数错误'	
        return HttpResponse(json.dumps(data), content_type="application/json") 
















